package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.event.InformUpdate;
import com.avic.rong.userOrder.bean.DiscountBean;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by XinEr on 2019/8/14.
 * Describe:
 */
public class DiscountListAdapter extends RecyclerView.Adapter<DiscountListAdapter.ViewHolder> {

    private Context context;
    private List<DiscountBean> discountList;


    public DiscountListAdapter(Context context, List<DiscountBean> discountList) {
        this.context = context;
        this.discountList=discountList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.discounts_list_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        DiscountBean discountBean = discountList.get(i);
        viewHolder.tv_price.setText("¥"+discountBean.getRange());
        viewHolder.tv_astrict.setText(discountBean.getMsg());
        viewHolder.tv_title.setText("使用范围："+discountBean.getScope());
        viewHolder.tv_time.setText("有效期限："+discountBean.getValidTime()+"");
//        优惠券类型0:折扣券.1:现金券
        if (discountBean.getCouponType()==0){
            viewHolder.iv_discounts_tag.setImageResource(R.mipmap.ic_cash);
        }else {
            viewHolder.iv_discounts_tag.setImageResource(R.mipmap.ic_full_reduction);
        }
        viewHolder.tv_use.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InformUpdate informUpdate = new InformUpdate();
                informUpdate.setEventType(informUpdate.CLICK_DISCOUNT_INFORM);
                informUpdate.setYouhuiJuanId(discountBean.getCouponId()+"");
                EventBus.getDefault().post(informUpdate);
            }
        });
    }

    @Override
    public int getItemCount() {
        return discountList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_price;
        private TextView tv_astrict;
        private LinearLayout ll_price;
        private TextView tv_title;
        private TextView tv_time;
        private TextView tv_use;
        private ImageView iv_discounts_tag;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_price=itemView.findViewById(R.id.tv_price);
            tv_astrict=itemView.findViewById(R.id.tv_astrict);
            ll_price=itemView.findViewById(R.id.ll_price);
            tv_title=itemView.findViewById(R.id.tv_title);
            tv_time=itemView.findViewById(R.id.tv_time);
            tv_use=itemView.findViewById(R.id.tv_use);
            iv_discounts_tag=itemView.findViewById(R.id.iv_discounts_tag);
        }
    }
}
